import {
  Controller,
  Get,
  Post,
  Body,
  Req,
  Res,
  Query,
  UseGuards
} from '@nestjs/common';

import { CreateItemDto } from './dto/create-item.dto';
import { Request, Response } from 'express';
import { QueryItemsDto } from './dto/query-items.dto';
import { TestService } from './test.service';
import { Item } from './interfaces/item.interface';
import { RequestUser } from '../common/interfaces/request.interface';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiUseTags } from '@nestjs/swagger';

@ApiUseTags('功能测试')
@ApiBearerAuth()
@Controller('test')
@UseGuards(AuthGuard())
export class TestController {
  constructor(private readonly testservice: TestService) {}

  /**
   * 查询所有的数据
   * @param queryItemsDto
   */
  @Post('query-all')
  findAll(@Body() queryItemsDto: QueryItemsDto): Promise<Item[]> {
    return this.testservice.findAll();
  }

  /**
   * 查询单个数据详情
   * @param req 请求体
   * @param res 响应体
   */
  @Get('query')
  async findOne(@Req() req: RequestUser): Promise<Item> {
    const user = req.user;
    return await this.testservice.findOne(req.query.id);
  }

  /**
   * 创建数据
   * @param createItemDto
   */
  @Post('create')
  createOne(@Body() createItemDto: CreateItemDto): Promise<Item> {
    return this.testservice.createOne(createItemDto);
  }

  /**
   * 更新数据
   */
  @Post('update')
  updateOne(@Body() updateItemDto): Promise<Item> {
    return this.testservice.updateOne(updateItemDto._id, updateItemDto);
  }

  /**
   * 删除数据
   */
  @Get('delete')
  deleteOne(@Req() req: Request): Promise<Item> {
    return this.testservice.deleteOne(req.query.id);
  }
}
